/**
 * 
 */
package sudoku.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;

import sudoku.controller.Controller;
import sudoku.model.SudokuFileFilter;
import sudoku.model.SudokuModel;
import sudoku.ui.GameChoiceUI;
import sudoku.ui.SudokuUI;
import sudoku.util.game.Database;

/**
 * @author Romet
 * 
 */
public class Data {

  private volatile Config config;
  private volatile SudokuUI m2ng;
  private volatile SudokuModel model;
  private volatile Controller controller;
  private volatile Language language;

  private BufferedReader lugeja;
  private PrintWriter kirjutaja;

  private String msave = "m2nguAjad.save";
  private String hsave = "ajalugu.save";

  private List<String> m2nguAjad9;
  private List<String> m2nguAjad16;
  private List<String> ajalugu;

  private int games;

  private JFileChooser fileChooser;

  private List<String> olemasolevadM2ngud9;
  private List<String> olemasolevadM2ngud16;

  public String[] tempm2ngud = {
      "8, , , ,7, , , ,6, ,3, , , ,9,8,2, , ,4, ,2,1, , , , , ,9, , , , ,1, , ,1, ,5, , , ,4, ,9, , ,6, , , , ,7, , , , , ,2,7, ,5, , ,5,2,6, , , ,9, ,3, , , ,8, , , ,7",
      "5, , ,8,3, , ,9, , ,4,6, , ,9, , , , ,9, , ,2, , , , , , ,7, ,8,4,5,3, , , , , ,1, , , , , ,3,1,2,6, ,4, , , , , , ,4, , ,6, , , , ,7, , ,2,8, , ,7, , ,5,2, , ,9",
      " ,6, , ,5, , ,4, , , ,1,9, ,7,6, , ,8, , , , , , , ,7,7, ,5, , , ,4, ,8, , , ,4, ,3, , , ,2, ,4, , , ,1, ,6,1, , , , , , , ,5, , ,3,2, ,8,9, , , ,9, , ,3, , ,2, ",
      // I
      "2, , , , ,1, , , , ,6, , ,8,5,1, , ,3, ,1, , ,6, ,7,2, , , , , ,2,4,8,7, , , , , , , , , ,1,2,6,7, , , , , ,8,9, ,6, , ,5, ,1, , ,7,9,1, , ,2, , , , ,8, , , , ,3",
      // G
      "2,4, , , , , ,6,1, , ,1, , , ,2, , , , ,8,1, ,4,5, , , ,1, ,8, ,2, ,5, ,3, , , ,1, , , ,2, ,9, ,5, ,7, ,4, , , ,4,9, ,3,8, , , , ,9, , , ,7, , ,1,3, , , , , ,9,5",
      // F
      " ,2, , ,7, , ,8, , , ,5, ,1,3, , , ,3,7, ,4, ,6,9, ,1, ,3,1, , , ,7, ,5,4, , ,5, ,2, , ,6,8, ,6, , , ,4,3, ,6, ,9,7, ,1, ,2,8, , , ,3,4, ,5, , , ,8, , ,6, ,1,7, ",
      // 167
      " , , , ,1, , ,3,4, , , ,4,9,5, , ,1, , ,1,3, ,8,6, ,5, ,1,4, , , ,5,6,3,9,8, ,2,5,3, ,1,7, ,5, ,1, ,4, ,2, , ,4, ,5,3,2, ,7, , ,6, , , , , ,5, ,8,3, ,9, ,6, ,4,2",
      // 166
      " , ,1,9,8,7,3, , , ,8, , , , , ,9, ,5, ,3,2, ,1,8, ,4,9, ,4,8, ,2,5, ,6,8, , , ,3, , , ,9,1, ,6,5, ,9,7, ,2,3, , ,6,2,8, , ,7, ,6, , , , , ,5, , ,4,8,1,9,5,6,2, ",
      // 129
      " , , , , , ,5, , , , , , ,9,2, , ,7, , , ,6,4,7,9, , , ,3,2, , , , , ,1,7,4, , , , , ,5,9,5, , , , , ,2,6, , , ,3,9,2,6, , , ,2, , ,7,3, , , , , , ,8, , , , , , ",
      // 128
      " , ,4, ,5, ,2, , , ,6,9, , , ,5,3, ,7, , , , , , , ,1, ,7, ,2, ,8, ,9, , , ,1,6, ,5,7, , , ,8, ,4, ,7, ,1, ,6, , , , , , , ,4, ,9,8, , , ,3,6, , , ,2, ,8, ,1, , ",
      // 127
      " , ,1,7, ,3,8, , , ,9,2, , , ,6,7, , , ,4, ,8, ,1, , , ,6, ,5, ,8, ,2, , , ,5, , , ,7, , , ,4, ,6, ,7, ,5, , , ,9, ,6, ,4, , , ,1, , , , , ,3, , , ,7,1, ,5,2, , ",
      // 126
      "6, , ,9,2, , ,1, , ,2, , ,3, , , , , , , , , ,7,4,2, , ,1, , , ,3,5, , ,9, , , ,6, , , ,4, , ,2,1, , , ,7, , ,9,1,3, , , , , , , , , ,7, , ,5, , ,3, , ,1,2, , ,8",
      // 125
      " ,8,1,6,5, ,4, , , , , , ,4, , ,3, ,6, , , , , ,9, ,5,5, , , , ,3, ,9, , , , , , , , , , , ,2, ,8, , , , ,7,2, ,6, , , , , ,3, ,5, , ,6, , , , , , ,9, ,7,1, ,2, ",
      // 124
      "2, , , , , ,9, , , , ,6, ,4,9, ,5, , , , ,2, , , , ,7, ,3, ,9, ,8, ,4, , , ,4, ,3, ,6, , , ,2, ,4, ,7, ,1, ,9, , , , ,3, , , , ,7, ,6,1, ,3, , , , ,8, , , , , ,5",
      // 123
      "8, , , ,3, , , ,6, , ,2, , , ,5, , , , , ,4, ,2, , , , ,4, ,7, ,3, ,8, , ,6,8, , , ,3,9, , ,9, ,6, ,4, ,2, , , , ,9, ,7, , , , , ,5, , , ,1, , ,1, , , ,5, , , ,2",
      // 122
      "4, ,2, ,6, ,3, ,5, ,6, ,2, ,5, ,1, ,3, , , , , , , ,9,9, , , , , , , ,1, ,3, ,1, ,9, ,2, ,1, , , , , , , ,6,2, , , , , , , ,3, ,8, ,4, ,3, ,9, ,6, ,3, ,1, ,4, ,8",
      // 121
      " , , , , , , ,8,4, , ,5, , ,2, , ,6, ,8,6,5, , ,1, , , ,4, ,1,9, , , ,2, , ,8, ,2, ,9, , ,5, , , ,7,3, ,4, , , ,4, , ,9, ,3, ,7, , ,8, , ,4, , ,8,9, , , , , , , ",
      // 120
      " , , ,1,7,4, , , ,5, ,3, ,2, ,4, ,9, , , , , , , , , ,7, ,6, , , ,8, ,2, , ,1,5, ,2,7, , ,3, ,4, , , ,1, ,6, , , , , , , , , ,9, ,7, ,8, ,6, ,4, , , ,6,3,9, , , ",
      // 3
      " , ,9, ,5,6, ,2,1, ,2,5,3,9, , ,7, ,3, , , , ,7, ,6, ,1, , , , ,8,6, , ,7,6,2, , , , ,5,8, , ,4,1, , ,7,3,9, ,4, ,7, , , , ,6, ,8, , ,2,5,4,1, ,2,3, ,4,1, ,5, , ",
      // 10
      " , , , ,8, ,2, ,7, ,2,6, ,5, , ,8, , ,7, ,6, ,1,4, ,3, , , ,5, , , ,3,9,2,1,7,3, ,6,8,4, ,5,9, , , ,7, , , ,3, ,2,9, ,4, ,1, , ,5, , ,3, ,6,9, ,9, ,8, ,6, , , , ",
      // 15
      " ,4,3, , , , ,5,9, , ,5, , , ,8,7,2, , , , ,6,5, , ,1, , ,2, ,8,4, ,3,6, ,1, ,5, ,9, ,8, ,4,8, ,3,7, ,2, , ,7, , ,8,9, , , , ,9,5,4, , , ,1, , ,6,2, , , , ,7,9, ",
      // 4
      "7, ,2, , , ,8,1, ,4, , ,3, ,1, , , ,1,9, , ,7, ,5, ,6, ,6, , , ,4,1, ,2, , ,1,7, ,5,9, , ,9, ,5,8, , , ,3, ,2, ,4, ,8, , ,9,1, , , ,6, ,2, , ,7, ,3,8, , , ,4, ,5",
      // 5
      " ,1,5,9, ,3, , ,2, , ,8,2, ,5,6,7, ,3, , , , ,4, ,1, ,2,3,4, , , ,7,6, , , , ,8,6,7, , , , ,8,6, , , ,1,5,9, ,4, ,3, , , , ,6, ,2,9,1, , ,5, , ,5, , ,7, ,9,8,4, ",
      // 6
      " ,6, ,7, ,5,2,3, ,4, ,5, , , , ,1, ,1,7, ,4, ,9, , , , ,5,3, ,4, ,1,7,2, , , , ,8, , , , ,2,1,9, ,7, ,6,4, , , ,1,8, ,7, ,6,3, ,8, , , , ,7, ,9, ,9,4,3, ,2, ,5, ",
      // http://www.sudoku.4thewww.com/other.php
      "1, , ,2,3,4, , ,12, ,6, , , ,7, , , ,8, , , ,7, , ,3, , ,9,10,6,11, ,12, , ,10, , ,1, ,13, ,11, , ,14, ,3, , ,15,2, , ,14, , , ,9, , ,12, ,13, , , ,8, , ,10, ,12,2, ,1,15, , , ,11,7,6, , , ,16, , , ,15, , ,5,13, , , ,10, ,5,15, , ,4, ,8, , ,11, ,16, , ,5,9,12, , ,1, , , , , ,8, , ,2, , , , , ,13, , ,12,5,8, , ,3, ,13, , ,15, ,3, , ,14,8, ,16, , , ,5,8, , ,1, , , ,2, , , ,13,9,15, , , ,12,4, ,6,16, ,13, , ,7, , , ,5, ,3, , ,12, , , ,6, , ,4,11, , ,16, ,7, , ,16, ,5, ,14, , ,1, , ,2, ,11,1,15,9, , ,13, , ,2, , , ,14, , , ,14, , , ,11, ,2, , ,13,3,5, , ,12",
      // http://www.sudokupuzzles.org/16x16-1.html
      " , , , , , , , , , , , , , , ,5, ,10, , , ,11,15, , ,3,14, , ,8, ,7, , ,15, , ,10,3, , ,8,2, , , , ,6, , , , , , , , , , , , , , , ,15, , , , , , , , , , ,11, , , , , , ,5,12, , ,9, , , , , , , ,2,7, , ,15,11, , , ,5, , , , , , ,14,4, , , , , , , , , ,7,5,8,6, , , , , , , , ,14,4, , , , , , , , , , , , , , ,2,15,1, , ,9, , , , , , , , , , , , , , , , ,6, , , , , ,14,6,13,3, , , , , , , , ,15,5,16,4, , , ,10, , , , ,3, , , , , , , , , , ,7,1,12,2,10,8, , , , ,15, , , , , ,13, , , , ,15,11, , , , ,12, , , , ,1, , , , ,6, , , , , , , " };

  /**
   * Seab üles Data klassi. Paneb paika filechooseri ja kui leiduvad salvestatud
   * mängud või ajalugu, siis loeb need sisse.
   * 
   * @param config
   *          konfiguratsiooni klassi isend
   */
  public Data(Config config, Language language) {
    this.config = config;
    this.language = language;

    fileChooser = new JFileChooser(config.getKaust());
    SudokuFileFilter filter = new SudokuFileFilter();
    filter.addExtension("sud");
    filter.setDescription("Sudoku failid");
    fileChooser.setFileFilter(filter);

    m2nguAjad9 = new ArrayList<String>();
    m2nguAjad16 = new ArrayList<String>();
    ajalugu = new ArrayList<String>();
    try {
      lugeja = new BufferedReader(new FileReader(msave));
      String rida = "";
      for (int i = 0; rida != null; i++) {
        if (i > 0) {
          if (rida.split("\t")[2].equals("9x9")) {
            m2nguAjad9.add(rida);
          } else {
            m2nguAjad16.add(rida);
          }
        }
        rida = lugeja.readLine();
      }
    } catch (FileNotFoundException er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    } catch (Exception er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    }
    try {
      lugeja = new BufferedReader(new FileReader(hsave));
      String rida = "";
      for (int i = 0; rida != null; i++) {
        if (i > 0) {
          ajalugu.add(rida);
        }
        rida = lugeja.readLine();
      }
    } catch (FileNotFoundException er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    } catch (Exception er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    }
    if (!this.config.isD0()) {
      version3();
      config.setD0(true);
    }
    if (!this.config.isV31()) {
      version31();
      config.setV31(true);
    }
    if (!this.config.isV34()) {
      version34();
      config.setV34(true);
    }
    if (!this.config.isV41()) {
      version41();
      config.setV41(true);
    }
    if (!this.config.isV413()) {
      version41();
      config.setV413(true);
    }
  }

  /**
   * Salvestatud mängu valimine. Avab salvestatud mängude kausta, mingi mängu
   * avamise korral annab mängu joonistamise üle modelile ja paneb paika failist
   * loetud timeri aja.
   */
  public void avaM2ng() {
    String aeg = "00:00:00";
    try {
      int returnVal = fileChooser.showDialog(m2ng, language.getButton_open());
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        lugeja = new BufferedReader(new FileReader(file));
        String esialgne = lugeja.readLine();
        String lahendus = lugeja.readLine();
        String abi = "";
        String rida = lugeja.readLine();
        if (rida.length() == 8) {
          aeg = rida;
          StringBuffer abib = new StringBuffer();
          for (int i = 0; i < config.getRuudustik() * config.getRuudustik(); i++) {
            abib.append(" ");
            if (i != config.getRuudustik() * config.getRuudustik() - 1) {
              abib.append(",");
            }
          }
          abi = abib.toString();
        } else {
          abi = rida;
          aeg = lugeja.readLine();
        }
        lugeja.close();
        File dir = fileChooser.getCurrentDirectory();
        config.setKaust(dir.getAbsolutePath());
        fileChooser.setCurrentDirectory(dir);
        controller.handleEvent(Controller.UUS_M2NG_EVENT);
        model.setNimi(file.getName());
        if (lahendus.split(";").length > 5) {
          model.avaM2ng(esialgne.split(";"), lahendus.split(";"), abi.split(";"));
        } else {
          model.avaM2ng(esialgne.split(","), lahendus.split(","), abi.split(";"));
        }
        m2ng.setTime_label(aeg);
        m2ng.seadistaLahendajad();
        m2ng.uuendaTitle();
        controller.handleEvent(Controller.ALUSTA_M2NG_EVENT);
        m2ng.seadistaUndoRedo();
      }
    } catch (FileNotFoundException er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    } catch (Exception er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    }
  }

  /**
   * Tagastab mängude ajaloo loetelu.
   * 
   * @return ajalugu
   */
  public List<String> getAjalugu() {
    return ajalugu;
  }

  public int getGames() {
    return games;
  }

  /**
   * Tagastab mängude aegade loetelu.
   * 
   * @return mängude ajad
   */
  public List<String> getM2nguAjad() {
    if (games == 9) {
      return m2nguAjad9;
    }
    return m2nguAjad16;
  }

  public List<String> getOlemasolevadM2ngud(int ruut) {
    if (ruut == 9) {
      return olemasolevadM2ngud9;
    }
    return olemasolevadM2ngud16;
  }

  public List<String> getOlemasolevadM2ngud16() {
    return olemasolevadM2ngud16;
  }

  public List<String> getOlemasolevadM2ngud9() {
    return olemasolevadM2ngud9;
  }

  /**
   * Salvestab antud mängu. Määrab salvestatava mängu nime ja salvestamise
   * asukoha. Salvestatud mängu failis asuvad esimesel real mängu algseisu
   * nuppude tekstid, teisel real juba lahendatud nuppude tekstid ja kolmandal
   * real abinupud ja neljandal timeri aeg.
   */
  public void salvestaM2ng() {
    try {
      int returnVal = fileChooser.showDialog(m2ng, language.getButton_save());
      fileChooser.setDialogTitle(language.getButton_save());
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        String fil = file.getAbsolutePath();
        boolean vaja = false;
        if (fil.length() < 4) {
          vaja = true;
        } else {
          String ext = fil.substring(fil.length() - 4, fil.length());
          if (!ext.equals(".sud")) {
            vaja = true;
          }
        }

        if (vaja) {
          file = new File(fil + ".sud");
        }
        StringBuffer esialgne = new StringBuffer();
        StringBuffer lahendus = new StringBuffer();
        StringBuffer abi = new StringBuffer();
        for (int i = 0; i < config.getRuudustik(); i++) {
          for (int j = 0; j < config.getRuudustik(); j++) {
            if (i != 0 || j != 0) {
              esialgne.append(",");
              lahendus.append(",");
              abi.append(";");
            }
            if (m2ng.getHelpButtonsCount(i, j) > 0) {
              lahendus.append(" ");
              esialgne.append(" ");
              for (int k = 0; k < m2ng.getRuut()[i][j].getComponentCount(); k++) {
                abi.append(((JLabel) m2ng.getRuut()[i][j].getComponent(k)).getText());
                if (k != m2ng.getRuut()[i][j].getComponentCount() - 1) {
                  abi.append(",");
                }
              }
            } else {
              if (model.getSudokuButtons()[i][j].getV2rv().equals(config.getEsialgne())) {
                esialgne.append(model.getSudokuButtons()[i][j].getNumber());
                lahendus.append(" ");
                abi.append(" ");
              } else {
                lahendus.append(model.getSudokuButtons()[i][j].getNumber());
                esialgne.append(" ");
                abi.append(" ");
              }
            }
          }
        }
        kirjutaja = new PrintWriter(new FileWriter(file));
        kirjutaja.println(esialgne.toString());
        kirjutaja.println(lahendus.toString());
        kirjutaja.println(abi.toString());
        kirjutaja.println(m2ng.getTime_label().getText());
        kirjutaja.flush();
        kirjutaja.close();
        File dir = fileChooser.getCurrentDirectory();
        config.setKaust(dir.getAbsolutePath());
        fileChooser.setCurrentDirectory(dir);
        model.setNimi(file.getName());
        m2ng.uuendaTitle();
      }
    } catch (FileNotFoundException er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    } catch (Exception er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    }
  }

  public void seaAndmebaas(GameChoiceUI gameChoiceUI) {
    JDialog d = new JDialog(m2ng);
    d.add(new JLabel(language.getUpdater_wait()));
    d.setUndecorated(true);
    d.pack();
    d.setVisible(true);
    d.setLocation(m2ng.getLocation().x + m2ng.getWidth() / 2 - d.getWidth() / 2,
        m2ng.getLocation().y + m2ng.getHeight() / 2 - d.getHeight() / 2);
    m2ng.setEnabled(false);
    Database database = new Database("set", d);
    database.setM2ng(m2ng);
    database.setModel(model);
    database.setConfig(config);
    database.setLanguage(language);
    database.setData(this);
    database.setGameChoiceUI(gameChoiceUI);
    new Thread(database).start();
  }

  /**
   * Sätib mängude ajaloo loetelu ja salvestab selle vastavasse faili.
   * 
   * @param ajalugu
   */
  public void setAjalugu(List<String> ajalugu) {
    this.ajalugu = ajalugu;
    Collections.sort(this.ajalugu);
    try {
      kirjutaja = new PrintWriter(new FileWriter(hsave));
      for (int i = 0; i < this.ajalugu.size(); i++) {
        kirjutaja.println(this.ajalugu.get(i));
        kirjutaja.flush();
      }
      kirjutaja.close();
    } catch (FileNotFoundException er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    } catch (Exception er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    }
  }

  public void setController(Controller controller) {
    this.controller = controller;
  }

  public void setGames(int games) {
    this.games = games;
  }

  /**
   * Määrab andmetes ära põhiakna loogika isendi.
   * 
   * @param model
   *          põhiakna loogika
   */
  public void setModel(SudokuModel model) {
    this.model = model;
  }

  /**
   * Määrab andmetes ära põhiakna graafika isendi.
   * 
   * @param m2ng
   *          mängu põhiakna graafika
   */
  public void setM2ng(SudokuUI m2ng) {
    this.m2ng = m2ng;
  }

  /**
   * Sätib mängude ajad ja salvestab need faili.
   * 
   * @param m2nguAjad
   *          mängude ajad
   */
  public void setM2nguAjad(List<String> m2nguAjad) {
    if (config.getRuudustik() == 9) {
      this.m2nguAjad9 = m2nguAjad;
    } else {
      this.m2nguAjad16 = m2nguAjad;
    }
    try {
      kirjutaja = new PrintWriter(new FileWriter(msave));
      for (int i = 0; i < this.m2nguAjad9.size(); i++) {
        kirjutaja.println(this.m2nguAjad9.get(i));
        kirjutaja.flush();
      }
      for (int i = 0; i < this.m2nguAjad16.size(); i++) {
        kirjutaja.println(this.m2nguAjad16.get(i));
        kirjutaja.flush();
      }
      kirjutaja.close();
    } catch (FileNotFoundException er) {
    } catch (Exception er) {
      Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
    }
  }

  public void setOlemasolevadM2ngud16(List<String> olemasolevadM2ngud16) {
    this.olemasolevadM2ngud16 = olemasolevadM2ngud16;
  }

  public void setOlemasolevadM2ngud9(List<String> olemasolevadM2ngud9) {
    this.olemasolevadM2ngud9 = olemasolevadM2ngud9;
  }

  /**
   * versioonile 3 üle minnes tuleb ajalugu puhastada
   */
  public void version3() {
    setAjalugu(new ArrayList<String>());
    setM2nguAjad(new ArrayList<String>());
  }

  /**
   * versioonile 4.1 üle minnes tuleb genereeritud mängudnullida
   */
  public void version41() {
    config.nulliGenereeritud();
  }

  /**
   * versioonile 3.1 üle minnes tuleb üliraskete mängude ajad kustutada
   */
  public void version31() {
    List<String> a = new ArrayList<String>();
    List<String> m = new ArrayList<String>();
    for (int i = 0; i < ajalugu.size(); i++) {
      String s = ajalugu.get(i);
      if (!s.contains("16x16") && s.contains(". Sudoku   ")) {
        if (s.contains("9x9   1. Sudoku") || s.contains("9x9   2. Sudoku") || s.contains("9x9   9. Sudoku")
            || s.contains("9x9   10. Sudoku") || s.contains("9x9   14. Sudoku") || s.contains("9x9   22. Sudoku")
            || (s.substring(6, 13)).split(" ")[0].compareTo("38.") > 0) {
          continue;
        }
      }
      a.add(s);
    }
    for (int i = 0; i < m2nguAjad9.size(); i++) {
      String s = m2nguAjad9.get(i);
      String ss = s.split("\t")[0];
      if (ss.equals("1") || ss.equals("2") || ss.equals("9") || ss.equals("10") || ss.equals("14") || ss.equals("22")
          || ss.compareTo("38") > 0) {
        continue;
      }
      m.add(s);
    }
    setAjalugu(a);
    int temp = config.getRuudustik();
    config.setRuudustik(9);
    setM2nguAjad(m);
    config.setRuudustik(temp);
    config.setDatabaseVersion("-999");
    config.nulliGenereeritud();
    File f = new File("sudoku.txt");
    if (f.exists()) {
      f.delete();
    }
  }

  private boolean version34M2ngudajadCheck(String st) {
    for (int i = 0; i < m2nguAjad9.size(); i++) {
      String s = m2nguAjad9.get(i);
      if (s.split("\t")[0].equals(st)) {
        return true;
      }
    }
    return false;
  }

  /**
   * versioonile 3.4 üle minnes tuleb taastada mängud, mis kustusid mängude
   * aegade tabelist 3.1 bugi tõttu
   */
  public void version34() {
    for (int i = 0; i < ajalugu.size(); i++) {
      String s = ajalugu.get(i);
      if (s.startsWith("9x9")) {
        String[] ss = s.substring(6).split("          ");
        if (ss[0].length() > 8 && ss[0].substring(ss[0].length() - 8).equals(". Sudoku")) {
          String st = ss[0].substring(0, ss[0].length() - 8);
          if (!version34M2ngudajadCheck(st)) {
            StringBuffer sb = new StringBuffer();
            sb.append(st);
            sb.append("\t");
            sb.append(ss[2]);
            sb.append("\t9x9");
            m2nguAjad9.add(sb.toString());
            try {
              kirjutaja = new PrintWriter(new FileWriter(msave));
              for (int j = 0; j < m2nguAjad9.size(); j++) {
                kirjutaja.println(m2nguAjad9.get(j));
                kirjutaja.flush();
              }
              for (int j = 0; j < m2nguAjad16.size(); j++) {
                kirjutaja.println(m2nguAjad16.get(j));
                kirjutaja.flush();
              }
              kirjutaja.close();
            } catch (FileNotFoundException er) {
            } catch (Exception er) {
              Config.log(er.getClass().toString() + ": " + er.getMessage(), er.getStackTrace());
            }
          }
        }
      }
    }
    config.setDatabaseVersion("-999");
    File f = new File("sudoku.txt");
    if (f.exists()) {
      f.delete();
    }
  }
}
